#conding:utf-8
import xlrd,xlwt
from xlutils.copy import copy
# import copy
from data.data_config import *


# data=xlrd.open_workbook('../dataconfig/test.xlsx')
# tables=data.sheets()[0]
# print(tables.cell_value(0,0))
# print(tables.nrows)


class OperationExcel:
    def __init__(self):
    # def __init__(self,file_name,sheet_id):
        self.file_name= 'D:/jiekkou/dataconfig/test.xls'
        self.sheet_id=0
        # if file_name !=None:
        #     self.file_name = file_name
        #     self.sheet_id = sheet_id
        #     self.data = self.get_data(file_name, sheet_id)
        # else:
        #     self.file_name='../dataconfig/test.xlsx'
        #     self.sheet_id=0
        self.data=self.get_data()
        self.write_data=global_var()

# 获取单元格
    def get_data(self):
        data=xlrd.open_workbook(self.file_name)
        tables=data.sheets()[self.sheet_id]
        return tables
# 获取行数
    def get_lines(self):
        tables=self.data
        return tables.nrows
# 获取单元格数据
    def get_cell_value(self,row,col):
        return self.data.cell_value(row,col)
# 写入数据(需要传入数据)
    def write_value(self,row,col,value):
        read_data=xlrd.open_workbook(self.file_name,formatting_info=False)
        write_data=copy(read_data)
        sheet_data=write_data.get_sheet(0)
        sheet_data.write(row,col,value)
        write_data.save(self.file_name)
        # print("12121")
    #  根据对应id获取对应数据
    def get_row_data(self,case_id):
        row_num=self.get_row_num(case_id)
        rows_data=self.get_row_values(row_num)
        # print(rows_data)
        return rows_data
    #根据对应的caseid找到对应行号
    def get_row_num(self,case_id):
        num=0
        clols_data=self.get_cols_data()
        for col_data in clols_data :
            if case_id in col_data:
                return num
            num=num+1
    #根据行号，找到该行的内容
    def get_row_values(self,row):
        table=self.data
        row_data=table.row_values(row)
        return row_data
    #获取某已列内容
    def get_cols_data(self,col_id=None):
        if col_id !=None:
            cols=self.data.col_values(col_id)
        else:
            cols=self.data.col_values(0)
        return cols
    # 写excel表中
    def write_result_excel(self,row,data):
        col=int(get_result())
        read_data=xlrd.open_workbook(self.file_name)
        write_data=copy(read_data)
        sheet_data=write_data.get_sheet(self.sheet_id)
        sheet_data.write(row,col,data)
        write_data.save(self.file_name)
if __name__=='__main__':
    oper=OperationExcel()
    # oper.write_value(2,10,"22121")
    # oper.get_row_data(1)
    # test=oper.get_cols_data()
    oper.write_excel(11,"21")
    # print(oper.get_cell_value(1,5))
    # print(test)

    # print(oper.get_row_data("test01"))
    # print(oper.get_row_num("test02"))
    # print(oper.get_row_values(oper.get_row_num("test02")))


    # print(oper.get_lines())
    # print(oper.get_data().nrows)